package com.jpaAutoProcess.selectKernel;


import com.jpaAutoProcess.utils.SqlUtils;
import com.jpaAutoProcess.utils.parseParamCondition.RequirParametersAndConditions;
import com.jpaAutoProcess.utils.type.Classtype;
import com.jpaAutoProcess.utils.ParamConditionBO;
import com.jpaAutoProcess.utils.type.Condition;
import com.jpaAutoProcess.utils.type.PropertyType;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * @version 1.1
 * @User: c.c
 * @Date: 2022/6/7
 * @Time: 11:31
 * @Description: 自动分页
 * 准备写多表，多条件复核查询
 */

public class KernelClass<T> {


    /**
     * @author: C.c
     * @date: 2022-06-09 17:03
     * @description: 根据不同条件，生成不同的queryQuery 根据情况进行分页，并返回数组
     * @version 1.3.1
     **/
    public static Object getresultlist(StringBuilder selectSql, StringBuilder fromsql, StringBuilder wheresql, Map<String, Object> params, Pageable pageable, EntityManager entityManager, Class<?> classtype,boolean islist) {
        Query queryQuery;
        List<Object> list;//判断select是哪种类型 是select * 还是 select name,sex
        if (selectSql.toString().contains(".*")){
            //判断sql与实体是否有映射
            if (haveAnnotation(classtype)){
                queryQuery = entityManager.createNativeQuery(selectSql.append(" ").append(fromsql).append(wheresql).toString(),classtype);
            }else {
                throw new RuntimeException("java对应实体没有与数据库字段进行映射，可以尝试不使用select xx.*");
            }
        }else {
            //类型是select name,sex 进行不同处理
            queryQuery = entityManager.createNativeQuery(selectSql.append(" ").append(fromsql).append(wheresql).toString());
            //设置要返回的类型

            queryQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(classtype));
        }
        if (pageable!=null&&!pageable.equals(" ")){
            queryQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            queryQuery.setMaxResults(pageable.getPageSize());
        }
        SqlUtils.setParameters(params, queryQuery);
        try {
            list = queryQuery.getResultList();
        }catch (Exception e){
            throw new RuntimeException("所提供的实体类型，与数据库字段不匹配");
        }
        //普通不分页查询单条数据显示
        if (!islist){
            return list.get(0);
        }

        return list;
    }

    //TODO 暂时没使用
    public List<T> getresultlist_test(StringBuilder selectSql, StringBuilder fromsql, StringBuilder wheresql, Map<String, Object> params, Pageable pageable, EntityManager entityManager, Class<?> classtype,boolean islist) {
        NativeQueryJpa nativeQueryJpa = new NativeQueryJpa(classtype);
        return nativeQueryJpa.getResultList(selectSql,fromsql,wheresql,params,entityManager);
    }


        public static Object getresultlist(StringBuilder selectsql, StringBuilder fromsql, StringBuilder wheresql,Class<?> classtype,EntityManager entityManager,Map<String, Object> params,boolean isList) {

        return getresultlist(selectsql, fromsql, wheresql,params,null,entityManager,classtype,isList);
    }



    /**
     *v1.2
     *  @author: C.c
     * @date: 2022-06-07 16:45
     * @description: 拼接sql，判断sql条件，放置参数
     **/
    public static void spliceSentencesAndPutParams(Map<String,Object> databaseParameters, StringBuilder wheresql, Map<String, Object> params) {

        if (databaseParameters!=null && databaseParameters.size()!=0) {
            wheresql.append(" where 1=1 ");
            databaseParameters.forEach((k,v)->{
                //解析字段和判断条件
                ParamConditionBO fieldAndCondition = ParamConditionBO.parse(k);
                //这个只是放置where参数和组装where语句 一般情况下参数不能为空。若判断条件是，“该字段”非空，“该字段”是空，那么就直接进入
                if (v!=null|| Condition.isNotNull.equals(fieldAndCondition.getCondition())||Condition.isNull.equals(fieldAndCondition.getCondition())){
                    putParams(fieldAndCondition,wheresql,params,v);
                }
            });
        }
    }


    /**
    * @author: C.c
    * @date: 2022-06-09 16:37
    * @description: 放置参数 并根据条件 进行不同拼接
    **/
    private static void putParams(ParamConditionBO fieldAndCondition,StringBuilder wheresql,Map<String, Object> params,Object v){
        //解析传来v值的数据类型
        PropertyType propertyType = Classtype.judgeClassProperty(v);
        String sqlfield = fieldAndCondition.getPropertyName();
        switch (fieldAndCondition.getCondition()){
            case eq:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" = :").append(sqlfield);
                wheresql.append(tempSql);
                //判断是否出现k值重复
                Object o = params.putIfAbsent(sqlfield, v);
                if (o!=null){
                    throw new RuntimeException(new StringBuilder(sqlfield).append(":该属性，这边建议使用in").toString());
                }
                break;
            }
            case like:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(sqlfield,v);
                break;
            }
            case startwith:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(sqlfield,v+"%");
                break;
            }
            case endwith:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(sqlfield,"%"+v);
                break;
            }
            case containing:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(sqlfield,"%"+v+"%");
                break;
            }

            case between:{
                //进一步进行拼接
                BaseBetweenTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case ge:{
                BaseGreaterEqualTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case le:{
                BaseLessEqualTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case gt:{
                BaseGreatThanTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case lt:{
                BaseLessThanTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case in:{
                BaseInTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case not:{
                StringBuilder tempSql =  new StringBuilder(" AND ").append(sqlfield).append(" <> :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(sqlfield,v);
                break;
            }
            case isNotNull:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" IS NOT NULL");
                wheresql.append(tempSql);
                break;
            }

            case isNull:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" IS NULL");
                wheresql.append(tempSql);
                break;
            }
            default:{
                throw new RuntimeException(new StringBuilder(sqlfield).append("参数：选择了不存在的条件类型").toString());
            }
        }
    }

    private static void putParamstest(ParamConditionBO fieldAndCondition,StringBuilder wheresql,Map<String, Object> params,Object v){
        //解析传来v值的数据类型
        PropertyType propertyType = Classtype.judgeClassProperty(v);
        String sqlfield = fieldAndCondition.getPropertyName();
        String onlyValeNanme = String.valueOf(v.hashCode());
        switch (fieldAndCondition.getCondition()){
            case eq:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" = :").append(onlyValeNanme);
                wheresql.append(tempSql);
                params.put(onlyValeNanme,v);
                break;
            }
            case like:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(onlyValeNanme,v);
                break;
            }
            case startwith:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(onlyValeNanme,v+"%");
                break;
            }
            case endwith:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(onlyValeNanme,"%"+v);
                break;
            }
            case containing:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" LIKE :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(onlyValeNanme,"%"+v+"%");
                break;
            }

            case between:{
                //进一步进行拼接
                BaseBetweenTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case ge:{
                BaseGreaterEqualTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case le:{
                BaseLessEqualTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case gt:{
                BaseGreatThanTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case lt:{
                BaseLessThanTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case in:{
                BaseInTypeGeneratedSql(propertyType,v,params,wheresql,sqlfield);
                break;
            }
            case not:{
                StringBuilder tempSql =  new StringBuilder(" AND ").append(sqlfield).append(" <> :").append(sqlfield);
                wheresql.append(tempSql);
                params.put(sqlfield,v);
                break;
            }
            case isNotNull:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" IS NOT NULL");
                wheresql.append(tempSql);
                break;
            }

            case isNull:{
                StringBuilder tempSql = new StringBuilder(" AND ").append(sqlfield).append(" IS NULL");
                wheresql.append(tempSql);
                break;
            }
            default:{
                throw new RuntimeException(new StringBuilder(sqlfield).append("参数：选择了不存在的条件类型").toString());
            }
        }
    }
    /**
     * @author: C.c
     * @date: 2022-06-09 08:46
     * @description: 基于in list数组不同数据，生成不同类型
     **/
    private static void BaseInTypeGeneratedSql(PropertyType propertyType, Object v, Map<String, Object> params, StringBuilder wheresql, String sqlfield) {
        if (v instanceof List){
            List list = (List) v;
            //判断数组内的类型
            switch (propertyType){
                case LocalDateTime:{
                    //转换成yyyy-MM-dd的String数组
                    List<String> timeStrings = new ArrayList<>();
                    ((List) v).forEach( param ->{
                        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        String time = format.format((LocalDateTime) param);
                        timeStrings.add(time);
                    });
                    spliceSentenceOnIn(params, wheresql, sqlfield,timeStrings);
                    break;
                }
                case Date:{
                    //转换成yyyy-MM-dd的String数组
                    List<String> timeStrings = new ArrayList<>();
                    ((List) v).forEach( param ->{
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        timeStrings.add(simpleDateFormat.format(param));
                    });
                    spliceSentenceOnIn(params, wheresql, sqlfield,timeStrings);
                    break;
                }
                default:{
                    spliceSentenceOnIn(params, wheresql, sqlfield,v);
                    break;
                }
            }
        }else {
            throw new RuntimeException("请传递list数组类型");
        }
    }


    /**
     * @author: C.c
     * @date: 2022-06-09 08:46
     * @description: 基于Less than <里不同数据，生成不同类型
     **/
    private static void BaseLessThanTypeGeneratedSql(PropertyType propertyType, Object v, Map<String, Object> params, StringBuilder wheresql, String sqlfield) {
        switch (propertyType){
            case LocalDateTime:{
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String time = format.format((LocalDateTime) v);
                spliceSentenceOnlessThan(params,wheresql,sqlfield,time);
                break;
            }
            case Date:{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format((Date)v);
                spliceSentenceOnlessThan(params,wheresql,sqlfield,time);
                break;
            }
//            case String:{
//                spliceSentenceOnlessThan(params,wheresql,sqlfield,(String)v);
//                break;
//            }
//            case Long:{
//                spliceSentenceOnlessThan(params,wheresql,sqlfield,(Long)v);
//                break;
//            }
//            case Integer:{
//                spliceSentenceOnlessThan(params,wheresql,sqlfield,(Integer)v);
//                break;
//            }
            default:{
                spliceSentenceOnlessThan(params,wheresql,sqlfield,v);
            }
        }
    }

    /**
     * @author: C.c
     * @date: 2022-06-09 08:46
     * @description: 基于great than >里不同数据，生成不同类型
     **/
    private static void BaseGreatThanTypeGeneratedSql(PropertyType propertyType, Object v, Map<String, Object> params, StringBuilder wheresql, String sqlfield) {
        switch (propertyType){
            case LocalDateTime:{
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String time = format.format((LocalDateTime) v);
                spliceSentenceOnGreatThan(params,wheresql,sqlfield,time);
                break;
            }
            case Date:{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format((Date)v);
                spliceSentenceOnGreatThan(params,wheresql,sqlfield,time);
                break;
            }
//            case String:{
//                spliceSentenceOnGreatThan(params,wheresql,sqlfield,(String)v);
//                break;
//            }
//            case Long:{
//                spliceSentenceOnGreatThan(params,wheresql,sqlfield,(Long)v);
//                break;
//            }
//            case Integer:{
//                spliceSentenceOnGreatThan(params,wheresql,sqlfield,(Integer)v);
//                break;
//            }
            default:{
                spliceSentenceOnGreatThan(params,wheresql,sqlfield,v);
                break;
            }
        }
    }


    /**
     * @author: C.c
     * @date: 2022-06-09 08:46
     * @description: 基于Less equal <=里不同数据，生成不同类型
     **/
    private static void BaseLessEqualTypeGeneratedSql(PropertyType propertyType, Object v, Map<String, Object> params, StringBuilder wheresql, String sqlfield) {
        switch (propertyType){
            case LocalDateTime:{
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String time = format.format((LocalDateTime) v);
                spliceSentenceOnLessEqual(params,wheresql,sqlfield,time);
                break;
            }
            case Date:{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format((Date)v);
                spliceSentenceOnLessEqual(params,wheresql,sqlfield,time);
                break;
            }
//            case String:{
//                spliceSentenceOnLessEqual(params,wheresql,sqlfield,(String)v);
//                break;
//            }
//            case Long:{
//                spliceSentenceOnLessEqual(params,wheresql,sqlfield,(Long)v);
//                break;
//            }
//            case Integer:{
//                spliceSentenceOnLessEqual(params,wheresql,sqlfield,(Integer)v);
//                break;
//            }
            default:{
                spliceSentenceOnLessEqual(params,wheresql,sqlfield,v);
                break;
            }
        }
    }



    /**
    * @author: C.c
    * @date: 2022-06-09 08:46
    * @description: 基于greater equal >=里不同数据，生成不同类型
    **/
    private static void BaseGreaterEqualTypeGeneratedSql(PropertyType propertyType, Object v, Map<String, Object> params, StringBuilder wheresql, String sqlfield) {
        switch (propertyType){
            case LocalDateTime:{
                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String time = format.format((LocalDateTime) v);
                spliceSentenceOnGreaterEqual(params,wheresql,sqlfield,time);
                break;
            }
            case Date:{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format((Date)v);
                spliceSentenceOnGreaterEqual(params,wheresql,sqlfield,time);
                break;
            }
//            case String:{
//                spliceSentenceOnGreaterEqual(params,wheresql,sqlfield,(String)v);
//                break;
//            }
//            case Long:{
//                spliceSentenceOnGreaterEqual(params,wheresql,sqlfield,(Long)v);
//                break;
//            }
//            case Integer:{
//                spliceSentenceOnGreaterEqual(params,wheresql,sqlfield,(Integer)v);
//                break;
//            }
            default:{
                spliceSentenceOnGreaterEqual(params,wheresql,sqlfield,v);
                break;
            }
        }
    }


    /**
     * @author: C.c
     * @date: 2022-06-08 15:59
     * @description: 基于between里不同数据，生成不同类型
     **/
    private static void BaseBetweenTypeGeneratedSql(PropertyType propertyType, Object v, Map<String, Object> params, StringBuilder wheresql, String sqlfield) {
        //判断v是否是list
        if (v instanceof List) {
            if (((List) v).size()!=2){
                throw new RuntimeException("between条件应为两条");
            }
            switch (propertyType) {
                case LocalDateTime: {
                    //把数组取出来
                    LocalDateTime param1 = (LocalDateTime) ((List) v).get(0);
                    LocalDateTime param2 = (LocalDateTime) ((List) v).get(1);
                    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    String starttime = format.format(param1);
                    String endtime = format.format(param2);

                    spliceSentenceOnBetween(params, wheresql, sqlfield, starttime, endtime);

                    break;
                }

                case Date:{
                    Date param1 = (Date) ((List) v).get(0);
                    Date param2 = (Date) ((List) v).get(1);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String starttime = simpleDateFormat.format(param1);
                    String endtime = simpleDateFormat.format(param2);
                    spliceSentenceOnBetween(params, wheresql, sqlfield, starttime, endtime);
                    break;
                }
                case Integer:{
                    Integer num1 = (Integer) ((List) v).get(0);
                    Integer num2 = (Integer) ((List) v).get(1);
                    spliceSentenceOnBetween(params,wheresql,sqlfield,num1,num2);
                    break;
                }
                case Long:{
                    Long long1 = (Long) ((List) v).get(0);
                    Long long2 = (Long) ((List) v).get(1);
                    spliceSentenceOnBetween(params,wheresql,sqlfield,long1,long2);
                    break;
                }
                case BigDecimal:{
                    BigInteger bigInteger1 = (BigInteger) ((List) v).get(0);
                    BigInteger bigInteger2 = (BigInteger) ((List) v).get(1);
                    spliceSentenceOnBetween(params,wheresql,sqlfield,bigInteger1,bigInteger2);
                    break;
                }
                case String:{
                    String string1 = (String) ((List) v).get(0);
                    String string2 = (String) ((List) v).get(1);
                    spliceSentenceOnBetween(params,wheresql,sqlfield,string1,string2);
                    break;
                }
                default:{
                    Object o1 = ((List) v).get(0);
                    Object o2 = ((List) v).get(1);
                    spliceSentenceOnBetween(params,wheresql,sqlfield,o1,o2);
                    break;
                }
            }
        }


    }


    /**
    * @author: C.c
    * @date: 2022-06-08 17:38
    * @description: between条件拼接 根據參數 动态拼接<= 和 >=
    **/
    private static void spliceSentenceOnBetween(Map<String, Object> params, StringBuilder wheresql, String sqlfield, Object param1, Object param2) {
        StringBuilder sql1 = new StringBuilder(" AND ").append(sqlfield).append(" >= :").append(sqlfield);
        params.put(sqlfield, param1);

        //因为map k值唯一，所以需要做特殊处理
        StringBuilder sqlfield2 = new StringBuilder(sqlfield).append("1");
        StringBuilder sql2 = new StringBuilder(" AND ").append(sqlfield).append(" <= :").append(sqlfield2);
        params.put(sqlfield2.toString(), param2);

        wheresql.append(sql1).append(sql2);
    }


    /**
     * @author: C.c
     * @date: 2022-06-09 08:53
     * @description: greater equal条件拼接 根據參數 动态拼接>=
     **/
    private static void spliceSentenceOnGreaterEqual(Map<String, Object> params, StringBuilder wheresql, String sqlfield, Object param) {
        String onlyValue = new StringBuilder(sqlfield).append(param.hashCode()).toString();
        StringBuilder sql = new StringBuilder(" And ").append(sqlfield).append(" >= :").append(onlyValue);
        params.put(onlyValue,param);
        wheresql.append(sql);

//
//        StringBuilder sql = new StringBuilder(" And ").append(sqlfield).append(" >= :").append(onlyValue);
//        params.put(sqlfield,param);
//        wheresql.append(sql);
    }

    /**
     * @author: C.c
     * @date: 2022-06-09 08:53
     * @description: less equal条件拼接 根據參數 动态拼接<=
     **/
    private static void spliceSentenceOnLessEqual(Map<String, Object> params, StringBuilder wheresql, String sqlfield, Object param) {
        String onlyValue = new StringBuilder(sqlfield).append(param.hashCode()).toString();
        StringBuilder sql = new StringBuilder(" And ").append(sqlfield).append(" <= :").append(onlyValue);
        params.put(onlyValue,param);
        wheresql.append(sql);
    }


    /**
     * @author: C.c
     * @date: 2022-06-09 08:53
     * @description: grate than条件拼接 根據參數 动态拼接 >
     **/
    private static void spliceSentenceOnGreatThan(Map<String, Object> params, StringBuilder wheresql, String sqlfield, Object param) {
        String onlyValue = new StringBuilder(sqlfield).append(param.hashCode()).toString();
        StringBuilder sql = new StringBuilder(" And ").append(sqlfield).append(" > :").append(onlyValue);
        params.put(onlyValue,param);
        wheresql.append(sql);
    }

    /**
     * @author: C.c
     * @date: 2022-06-09 08:53
     * @description: less than条件拼接 根據參數 动态拼接 <
     **/
    private static void spliceSentenceOnlessThan(Map<String, Object> params, StringBuilder wheresql, String sqlfield, Object param) {
        String onlyValue = new StringBuilder(sqlfield).append(param.hashCode()).toString();
        StringBuilder sql = new StringBuilder(" And ").append(sqlfield).append(" < :").append(onlyValue);
        params.put(onlyValue,param);
        wheresql.append(sql);
    }

    /**
     * @author: C.c
     * @date: 2022-06-09 08:53
     * @description: in条件拼接 根據參數 动态拼接 in
     **/
    private static void spliceSentenceOnIn(Map<String, Object> params, StringBuilder wheresql, String sqlfield, Object param) {
        StringBuilder sql = new StringBuilder(" And ").append(sqlfield).append(" in :").append(sqlfield);
        params.put(sqlfield,param);
        wheresql.append(sql);
    }

    private static Object reflectObject(String paramname,Object o)  {
        Class<?> aClass = o.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        String property = paramname.toLowerCase();
        //取具体值
        for (Field declaredField : declaredFields) {
            if (declaredField.getName().contains(property)){
                declaredField.setAccessible(true);
                Object result = null;
                try {
                    result = declaredField.get(o);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                return result;
            }
        }
        return null;
    }

    /**
    * @author: C.c
    * @date: 2022-06-09 15:20
    * @description: 判断实体是否有映射
    **/
    private static Boolean haveAnnotation(Class a){
        Annotation[] annotations = a.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof Entity){
                return true;
            }

        }
        return false;
    }

    /**
     *v1.1 （废弃）已经整合到 {@link #spliceSentencesAndPutParams(Map, StringBuilder, Map)}
     *  @author: C.c
     * @date: 2022-06-07 16:45
     * @description: 第一层map k对应数据库名字 v值为第二次hashmap
     * 第二层hashmap k值为具体查询的值 v为条件
     * @Deprecated 第一版
     **/
    @Deprecated
    private static void spliceSentencesoneAndPutParamsone(Map<String, Object> databaseParameters, StringBuilder wheresql, Map<String, Object> params) {
        if (databaseParameters.size()!=0){
            wheresql.append(" where 1=1 ");

            //循环拿到map参数
            databaseParameters.forEach((k,v)->{
                //根据map有什么数据一一进行拼接
                //判断是否是日期
                if (k.contentEquals("")){

                    //第一版
                }else {
                    String sqlparams = k.toString();
                    if (v!=null){
                        StringBuilder tempSql = new StringBuilder(" AND ").append(sqlparams).append(" LIKE :").append(sqlparams);
                        wheresql.append(tempSql);
                        params.put(sqlparams,"%"+ v+"%");
                    }
                }

            });
        }
    }
    /**
     * @version 1.0
     * @author: C.c
     * @date: 2022-06-07 11:41
     * @description: 单表条件查询分页（已经整合到Autopage）
     *
     * @param select (select xxx ,xxx 注意：尽量别写select *)
     * @param from 条件 (from xxx )
     * @param databaseParameters 类型为AutoPageParametersConditionsMap
     * @param pageable 分页相关参数
     * @param entityManager sql执行
     * @param classtype 要转换成什么类型
     **/
    @Deprecated
    public static Page singtable(String select, String from, RequirParametersAndConditions<ParamConditionBO, Object> databaseParameters, Pageable pageable, EntityManager entityManager, Class<?> classtype){
        StringBuilder selectSql = new StringBuilder();
        StringBuilder fromsql = new StringBuilder();
        StringBuilder wheresql = new StringBuilder();
        StringBuilder countSql = new StringBuilder("select count(*) ");
        Map<String, Object> params = new HashMap<>();
        selectSql.append(select);
        fromsql.append(from);


        spliceSentencesAndPutParams(databaseParameters, wheresql, params);


        Query countQuery = entityManager.createNativeQuery(countSql.append(from).toString());
        SqlUtils.setParameters(params,countQuery);
        long count = ((BigInteger) countQuery.getSingleResult()).longValue();
        List<Object> list = new ArrayList<>();
        if (count > 0) {
            wheresql.append(" ORDER BY ctime DESC ");


            Query queryQuery = null;
            //判断select是哪种类型 是select * 还是 select name,sex
            if (select.contains(".*")){
                //判断sql与实体是否有映射
                if (haveAnnotation(classtype)){
                    queryQuery = entityManager.createNativeQuery(selectSql.append(" ").append(from).append(wheresql).toString(),classtype);
                }else {
                    throw new RuntimeException("java对应实体没有与数据库字段映射");
                }
            }else {
                //类型是select name,sex 进行不同处理
                queryQuery = entityManager.createNativeQuery(selectSql.append(" ").append(from).append(wheresql).toString());
                queryQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.aliasToBean(classtype));
            }
            queryQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            queryQuery.setMaxResults(pageable.getPageSize());
            SqlUtils.setParameters(params, queryQuery);
            list = queryQuery.getResultList();
        }

        return new PageImpl<>(list, pageable, count);
    }

}
